/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：UIFormBase类的编辑器扩展
* CreateTime：2020-04-05 18:52:29
* Version：1.0
* Modify Recoder：
*************************************************/

using System;
using TinyToolKit.UI;
using UnityEditor;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine;

namespace TinyToolKitEditor
{
    [CustomEditor(typeof(UIFormBase), true)]
    public class UIFormBaseInspector : EditorBase
    {
        private UIFormBase _target => target as UIFormBase;
        
        private SerializedProperty _uiFormLayerType;
        private SerializedProperty _uiFormShowType;
        private SerializedProperty _uiFormRaycastType;
        private SerializedProperty _isClearOrder;

        private List<SerializedProperty> _childProperties = new List<SerializedProperty>();   

        protected override void OnEnable() 
        {
            base.OnEnable();

            _uiFormLayerType = serializedObject.FindProperty("UIFormLayerType");
            _uiFormShowType = serializedObject.FindProperty("UIFormShowType");
            _uiFormRaycastType = serializedObject.FindProperty("UIFormRaycastType");
            _isClearOrder = serializedObject.FindProperty("isClearOrder");

            FieldInfo[] fields = serializedObject.targetObject.GetType().GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                _childProperties.Add(serializedObject.FindProperty(fields[i].Name));
            }
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();

            EditorGUILayout.PropertyField(_uiFormLayerType,new GUIContent("Layer"));
            if(_uiFormShowType.enumValueIndex == 2 && _uiFormLayerType.enumValueIndex != 2)
                EditorGUILayout.HelpBox("当前为模态窗体，建议将该窗体放置于PopUp层中",MessageType.Warning);

            EditorGUILayout.PropertyField(_uiFormShowType,new GUIContent("Display Mode")); 
            if (_uiFormShowType.enumValueIndex == 2)
                EditorGUILayout.PropertyField(_uiFormRaycastType,new GUIContent("  Raycast"));
            if (_uiFormShowType.enumValueIndex == 2)
                EditorGUILayout.PropertyField(_isClearOrder,new GUIContent("  Close Others"));  
            
            EditorGUILayout.LabelField("State",Enum.GetName(typeof(UIFormStateType),_target.GetStateType));
            EditorGUILayout.LabelField("",new GUIStyle("EyeDropperHorizontalLine"));

            if (_childProperties != null && _childProperties.Count > 0)
            {
                for (int i = 0; i < _childProperties.Count; i++)
                {
                    EditorGUILayout.PropertyField(_childProperties[i]);
                }
            }
            
            serializedObject.ApplyModifiedProperties();
        }
    }
}
